package com.huichaoliu.stylist.activity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

import com.huichaoliu.app.R;

public class StylistShoppingActivity extends FragmentActivity implements
		OnClickListener {
	// tools
	Context context;
	FragmentManager fm;

	// views
	TextView view_all;
	TextView view_clothes;
	TextView view_pants;
	TextView view_shoes;

	// fragment
	ShoppingIncludeFragment2 fragmentAll;
	ShoppingIncludeFragment2 fragmentClothes;
	ShoppingIncludeFragment2 fragmentPants;
	ShoppingIncludeFragment2 fragmentShoes;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.stylist_shopping_layout);
		super.onCreate(savedInstanceState);

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		}

		init();
		initData();
	}

	public void init() {
		context = this;
		fm = ((FragmentActivity) context).getSupportFragmentManager();

		findViewById(R.id.stylist_back).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						finish();
					}
				});
		((TextView) findViewById(R.id.stylist_name)).setText("商城");
		view_all = (TextView) findViewById(R.id.shopping_all);
		view_clothes = (TextView) findViewById(R.id.shopping_clothes);
		view_pants = (TextView) findViewById(R.id.shopping_pants);
		view_shoes = (TextView) findViewById(R.id.shopping_shoes);

		view_all.setOnClickListener(this);
		view_clothes.setOnClickListener(this);
		view_pants.setOnClickListener(this);
		view_shoes.setOnClickListener(this);

		changePosition(0);
	}

	public void initData() {
		showAll();
	}

	@SuppressWarnings("deprecation")
	private void changePosition(int code) {
		view_all.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.stylist_showpic_p));
		view_clothes.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.stylist_showpic_p));
		view_pants.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.stylist_showpic_p));
		view_shoes.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.stylist_showpic_p));

		switch (code) {
		case 0:
			view_all.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_n));
			break;
		case 1:
			view_clothes.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_n));
			break;
		case 2:
			view_pants.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_n));
			break;
		case 3:
			view_shoes.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_n));
			break;
		default:
			break;
		}
	}

	private void showAll() {
		FragmentTransaction ft = fm.beginTransaction();
		if (fragmentAll == null) {
			fragmentAll = new ShoppingIncludeFragment2();
		}

		if (fragmentAll.isAdded()) {
			ft.show(fragmentAll);
		} else {
			ft.add(R.id.shopping_fragment, fragmentAll);
			ft.show(fragmentAll);
		}

		ft.commit();
		fragmentAll.setText("确认加入衣柜");

	}

	private void showClothes() {
		FragmentTransaction ft = fm.beginTransaction();
		if (fragmentClothes == null) {
			fragmentClothes = new ShoppingIncludeFragment2();
		}

		if (fragmentClothes.isAdded()) {
			ft.show(fragmentClothes);
		} else {
			ft.add(R.id.shopping_fragment, fragmentClothes);
			ft.show(fragmentClothes);
		}

		ft.commit();
		fragmentClothes.setText("确认加入衣柜");
	}

	private void showPants() {
		FragmentTransaction ft = fm.beginTransaction();
		if (fragmentPants == null) {
			fragmentPants = new ShoppingIncludeFragment2();
		}

		if (fragmentPants.isAdded()) {
			ft.show(fragmentPants);
		} else {
			ft.add(R.id.shopping_fragment, fragmentPants);
			ft.show(fragmentPants);
		}

		ft.commit();
		fragmentPants.setText("确认加入衣柜");
	}

	private void showShoes() {
		FragmentTransaction ft = fm.beginTransaction();
		if (fragmentShoes == null) {
			fragmentShoes = new ShoppingIncludeFragment2();
		}

		if (fragmentShoes.isAdded()) {
			ft.show(fragmentShoes);
		} else {
			ft.add(R.id.shopping_fragment, fragmentShoes);
			ft.show(fragmentShoes);
		}

		ft.commit();
		fragmentShoes.setText("确认加入衣柜");
	}

	private void hideAll() {
		FragmentTransaction ft = fm.beginTransaction();
		if (fragmentAll != null && fragmentAll.isAdded()) {
			ft.hide(fragmentAll);
		}
		if (fragmentClothes != null && fragmentClothes.isAdded()) {
			ft.hide(fragmentClothes);
		}
		if (fragmentPants != null && fragmentPants.isAdded()) {
			ft.hide(fragmentPants);
		}
		if (fragmentShoes != null && fragmentShoes.isAdded()) {
			ft.hide(fragmentShoes);
		}
		ft.commit();

	}

	@Override
	public void onClick(View arg0) {
		hideAll();
		switch (arg0.getId()) {
		case R.id.shopping_all:
			changePosition(0);
			showAll();
			break;
		case R.id.shopping_clothes:
			changePosition(1);
			showClothes();
			break;
		case R.id.shopping_pants:
			changePosition(2);
			showPants();
			break;
		case R.id.shopping_shoes:
			changePosition(3);
			showShoes();
			break;

		default:
			break;
		}
	}

	public void dissmissAll() {
		if (fragmentAll != null) {
			fragmentAll.dissmissAll();
		}
		if (fragmentClothes != null) {
			fragmentClothes.dissmissAll();
		}
		if (fragmentPants != null) {
			fragmentPants.dissmissAll();
		}
		if (fragmentShoes != null) {
			fragmentShoes.dissmissAll();
		}
	}

}
